package org.tom.pet.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.tom.basic.exception.MyException;
import org.tom.basic.util.JsonResult;
import org.tom.basic.util.LoginContextUtil;
import org.tom.basic.util.PageList;
import org.tom.org.domain.Employee;
import org.tom.org.mapper.EmployeeMapper;
import org.tom.org.service.IEmployeeService;
import org.tom.pet.constant.PetConstant;
import org.tom.pet.domain.Pet;
import org.tom.pet.query.PetQuery;
import org.tom.pet.service.IPetService;
import org.tom.user.domain.Logininfo;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/pet")
public class PetController {

    @Autowired
    private IPetService petService;

    @Autowired
    private EmployeeMapper employeeMapper;

    /**
     * 查询所有属于商城的宠物
     * @param query
     * @return
     */
    @PostMapping
    public PageList<Pet> loadAll(@RequestBody PetQuery query, HttpServletRequest request){
        Logininfo logininfo = LoginContextUtil.getLoginContext(request);
        Employee employee = employeeMapper.findByLogininfoId(logininfo.getId());
        if (employee.getShop_id() == null){
            return petService.queryList(query);
        }
        return null;
    }

    /**
     * 商城的宠物上架处理
     * @param ids
     * @return
     */
    @PostMapping("/onsale")
    public JsonResult onsale(@RequestBody List<Long> ids){
        try {
            return petService.onsale(ids);
        }catch (MyException e){
          e.printStackTrace();
            return JsonResult.me().setMsg(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("后端系统异常");
        }

    }

    /**
     * 商城宠物下架
     * @param ids
     * @return
     */
    @PostMapping("/offsale")
    public JsonResult offsale(@RequestBody List<Long> ids){
        try {
            return petService.offsale(ids);
        }catch (MyException e){
            e.printStackTrace();
            return JsonResult.me().setMsg(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("后端系统异常");
        }

    }

    @PostMapping("/list")
    public PageList<Pet> loadPetAll(@RequestBody PetQuery query){
        // 为了防止别人直接通过postman发送请求查询未上线的数据，继续加一层判断
        query.setState(1L);
        return petService.loadPetAll(query);
    }

    /**
     * 获取宠物详情
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public JsonResult loadOnPetByPetId(@PathVariable("id") Long id){
        try {
            // 为了防止别人直接通过postman发送请求查询未上线的数据，继续加一层判断，只允许查询上线的
            return petService.loadOnPetByPetId(id, PetConstant.ONLINE);
        }catch (MyException e){
            e.printStackTrace();
            return JsonResult.me().setMsg(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("后端系统异常");
        }
    }


    @GetMapping("/address")
    public JsonResult getAddress(){
        try {
            Logininfo logininfo = LoginContextUtil.getLoginContext();
            // 为了防止别人直接通过postman发送请求查询未上线的数据，继续加一层判断，只允许查询上线的
            return petService.getAddress(logininfo.getId());
        }catch (MyException e){
            e.printStackTrace();
            return JsonResult.me().setMsg(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("后端系统异常");
        }
    }

}
